Plots.jl is a non-traditional plotting library
The rapidly growing documentation is at http://docs.juliaplots.org/latest/
Plots.jl uses other plotting libraries as backends
UnicodePlots: Plots to unicode for no-display situations
Each of those is a separate plotting package, with a separate API. Each has their strengths and weaknesses. However, you'd have to learn 5 API's to use them all. Plots.jl reduces that to 1 API.
We will mostly rely on the default option, which is GR. More about Backends.
using Plots
plot(rand(4,4)) # will use the default backend: GR
pyplot() # change backend
plot(rand(4,4))
INFO: Recompiling stale cache file /Users/74097/.julia/lib/v0.6/PyPlot.ji for module PyPlot.
plotlyjs() # change backend
plot(rand(4,4))
Plotly javascript loaded.
To load again call
init_notebook(true)
* The PGFPlots backend can emit native `.tex` code.
* It's just plain beautiful.
pgfplots()
plot(rand(4,4))
using LaTeXStrings
plot([plot(sin,0,10,label=L"$\sin(x)$"),plot(x->sqrt(2x),0,10,label=L"$\sqrt{2x}$")]...)
dd = vcat([[sin(i) sqrt(2i)] for i in 0:0.01:10]...)
p = plot(dd,label=[L"$\sin(x)$",L"$\sqrt{2x}$"])
# save this:
savefig("myfile.tex")
# savefig("myfile.pdf")
# savefig("myfile.svg")
The attributes work with each of the backends: http://docs.juliaplots.org/latest/attributes/
Compatibility of attributes is found in this chart: http://docs.juliaplots.org/latest/supported/
I find it easiest to use this page to find the right attributes: http://docs.juliaplots.org/latest/examples/pyplot/
gr() # back to GR
plot(rand(4,4),title="Test Title",label=["First" "Second" "Third" "Fourth"])
Let's try this out. Most of those examples come from the examples section of the plots website, so check it out for more.
# lesson 1: every column is a series
plot(rand(10)) # 1 col = 1 series
plot(rand(10,2)) # 2 cols = ...
#Â different linetypes
plot(rand(10,2),line=(:dot,:auto),marker=([:circle :diamond]),color=[:green :orange])
# histogram
histogram(randn(1000),nbins=20,legend=false,title="My Histogram!",ylabel="counts")
#Â add to an existing plot later...
plot(rand(100) / 3,reg=true,fill=(0,:red))
# ... with plot! or scatter!
scatter!(rand(100),marker=(2,:circle),color=:black)
layout argument that you can specify.plot(rand(100,4),layout = 4,legend=false) # make 4 equal sized subplots
# specify the size of subplots
l = @layout([a{0.1h};b [c d; e]])
plot(randn(100,5),layout=l,t=[:line :histogram :scatter :steppre :bar],leg=false,ticks=nothing,border=false)
# we can also sequentially build plots and then stack them together
ty = [:line :histogram :scatter :steppre :bar]
p = Any[]
for typ in ty
push!(p,plot(rand(100),t=typ,title="$typ plot"))
end
plot(p...)
# ... and we can also add to the subplots in the same way
plot!(rand(100,5),t=:scatter)
# 3D plots
n = 100
ts = linspace(0,8Ï€,n)
x = ts .* map(cos,ts)
y = (0.1ts) .* map(sin,ts)
z = 1:n
plot(x,y,z,zcolor=reverse(z),m=(10,0.8,:blues,stroke(0)),leg=false,cbar=true,w=5)
plot!(zeros(n),zeros(n),1:n,w=10)
# plotlyjs is hard to beat for 3D
plotlyjs()
plot(x,y,z,zcolor=reverse(z),m=(10,0.8,:blues,stroke(0)),leg=false,cbar=true,w=5)
plot!(zeros(n),zeros(n),1:n,w=10)
Any plot can be animated: see https://juliaplots.github.io
Recipes are abstract instructions for how to "build a plot" from data. There are multiple kinds of recipes. In execution order:
Since these extend Plots.jl itself, all of Plots.jl is accessible from the plotting commands that these make, and these recipes are accessible from each other.
[Series recipes are used to extend the compatibility of backends itself!]
DifferentialEquations.jl by just calling plot(sol)# this is from http://docs.juliadiffeq.org/latest/tutorials/ode_example.html
using DifferentialEquations
f(u,p,t) = 1.01*u
u0=1/2
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob,Tsit5(),reltol=1e-8,abstol=1e-8)
gr() # switch backend again
plot(sol)
# the plot function still accepts keywords to set attributes:
plot(sol,linewidth=5,title="Solution to the linear ODE with a thicker line",
xaxis="Time (t)",yaxis="u(t) (in μm)",label="My Thick Line!")
# and we can add to the plot with plot!
plot!(sol.t, t->0.5*exp(1.01t),lw=3,ls=:dash,label="True Solution!")
plot methods at MomentOpt.jl@df macro# basic example
df = DataFrame(a = 1:10, b = 10*rand(10), c = 10 * rand(10))
@df df plot(:a, [:b :c], colour = [:red :blue])
@df df scatter(:a, :b, markersize = 4 * log.(:c + 0.1))
using RDatasets, StatPlots, Plots
iris = dataset("datasets","iris")
@df iris marginalhist(:PetalLength,:PetalWidth,bins=30)
WARNING: Keyword argument match_dimensions not supported with Plots.GRBackend(). Choose from: Set(Symbol[:top_margin, :group, :background_color, :yforeground_color_text, :yguidefontcolor, :seriesalpha, :legendfontcolor, :seriescolor, :ztick_direction, :zlims, :overwrite_figure, :xguidefonthalign, :normalize, :linestyle, :xflip, :fillcolor, :ygrid, :background_color_inside, :zguidefonthalign, :bins, :yscale, :xtickfontcolor, :xguide, :fillalpha, :tick_direction, :yguidefontsize, :legendfontfamily, :foreground_color, :xtickfonthalign, :x, :ytickfontrotation, :legend, :discrete_values, :ytick_direction, :xguidefontrotation, :ribbon, :tickfontrotation, :xdiscrete_values, :legendtitle, :xgridstyle, :orientation, :gridstyle, :markersize, :camera, :xforeground_color_grid, :quiver, :zticks, :markerstrokecolor, :ztickfontrotation, :ztickfonthalign, :legendfonthalign, :xtickfontsize, :levels, :zgridstyle, :foreground_color_border, :zguidefontvalign, :marker_z, :markerstrokealpha, :markeralpha, :tickfontvalign, :zguidefontcolor, :ygridlinewidth, :zlink, :zscale, :smooth, :xticks, :zguidefontsize, :y, :margin, :ytickfontcolor, :yforeground_color_border, :zguidefontfamily, :zgridalpha, :yguidefontvalign, :yguidefonthalign, :ztickfontcolor, :html_output_format, :tickfontcolor, :titlefontrotation, :legendfontvalign, :tickfontsize, :z, :yforeground_color_axis, :xtickfontrotation, :xerror, :xguidefontcolor, :primary, :guidefonthalign, :aspect_ratio, :link, :yguide, :guidefontvalign, :yguidefontfamily, :layout, :polar, :right_margin, :xlink, :series_annotations, :inset_subplots, :ytickfontsize, :tickfontfamily, :xgrid, :ygridalpha, :xtick_direction, :colorbar, :zflip, :ticks, :legendfontrotation, :linealpha, :arrow, :xtickfontvalign, :zgrid, :bar_width, :zguide, :zforeground_color_text, :weights, :xgridalpha, :ygridstyle, :fill_z, :ztickfontfamily, :markershape, :background_color_subplot, :xguidefontvalign, :markerstrokewidth, :xguidefontfamily, :gridlinewidth, :foreground_color_subplot, :xgridlinewidth, :foreground_color_text, :titlefonthalign, :yerror, :zgridlinewidth, :grid, :xguidefontsize, :xforeground_color_axis, :background_color_outside, :titlefontcolor, :line_z, :size, :projection, :zguidefontrotation, :ydiscrete_values, :seriestype, :yflip, :fillrange, :ztickfontvalign, :xlims, :xforeground_color_border, :markercolor, :ylink, :yforeground_color_grid, :color_palette, :lims, :xscale, :left_margin, :annotations, :window_title, :foreground_color_axis, :yguidefontrotation, :guidefontsize, :zdiscrete_values, :tickfonthalign, :bottom_margin, :framestyle, :scale, :zforeground_color_border, :background_color_legend, :linecolor, :foreground_color_legend, :title, :subplot_index, :flip, :titlefontvalign, :foreground_color_grid, :linewidth, :ztickfontsize, :gridalpha, :guidefontfamily, :ylims, :xtickfontfamily, :ytickfontvalign, :ytickfontfamily, :xforeground_color_text, :show, :guidefontrotation, :legendfontsize, :subplot, :label, :ytickfonthalign, :guide, :guidefontcolor, :titlefontsize, :titlefontfamily, :zforeground_color_axis, :zforeground_color_grid, :yticks])
# correlation plot from a dataframe
@df iris corrplot([:SepalLength :SepalWidth :PetalLength :PetalWidth], grid = false, bins=20)
# corrplot from a matrix
M = randn(1000,4)
M[:,2] += 0.8sqrt.(abs.(M[:,1])) - 0.5M[:,3] + 5
M[:,3] -= 0.7M[:,1].^2 + 2
corrplot(M, label = ["x$i" for i=1:4])
#Â cornerplot for same matrix
cornerplot(M)
school = RDatasets.dataset("mlmRev","Hsb82")
println(head(school))
@df school density(:MAch, group = :Sx)
6×8 DataFrames.DataFrame. Omitted printing of 1 columns │ Row │ School │ Minrty │ Sx │ SSS │ MAch │ MeanSES │ Sector │ ├─────┼────────┼────────┼────────┼────────┼────────┼───────────┼────────┤ │ 1 │ 1224 │ No │ Female │ -1.528 │ 5.876 │ -0.434383 │ Public │ │ 2 │ 1224 │ No │ Female │ -0.588 │ 19.708 │ -0.434383 │ Public │ │ 3 │ 1224 │ No │ Male │ -0.528 │ 20.349 │ -0.434383 │ Public │ │ 4 │ 1224 │ No │ Male │ -0.668 │ 8.781 │ -0.434383 │ Public │ │ 5 │ 1224 │ No │ Male │ -0.158 │ 17.898 │ -0.434383 │ Public │ │ 6 │ 1224 │ No │ Male │ 0.022 │ 4.583 │ -0.434383 │ Public │
# use tuple of col names to group by more
@df school density(:MAch, group = (:Sx, :Sector), legend = :topleft)
singers = RDatasets.dataset("lattice","singer")
@df singers violin(:VoicePart,:Height,marker=(0.2,:blue,stroke(0)))
@df singers boxplot!(:VoicePart,:Height,marker=(0.3,:orange,stroke(2)))
# there is great support to plot distributinos
using Distributions
plot(Normal(3,5), fill=(0, .5,:orange))
dist = Gumbel(2)
plot(dist,lw=3,label="pdf",legend=:right,title="Dig that plot!")
scatter!(dist,func=cdf,alpha=0.3,label="cdf",xlabel="x")
vline!([median(dist)],color=:red,lw=3,label="median")
groupedbar(rand(10,3), bar_position = :dodge, bar_width=0.7)
groupedbar(rand(10,3), bar_position = :dodge, bar_width=0.7)
df = DataFrame(a = 1:50, b = 10*rand(50), c = 10 * rand(50))
@df df scatter(:a, :b, markersize = 4 * log.(:c + 0.1)) # like before
using Query
df |>
@filter(_.a > 5) |>
@map({_.b, d = _.c-10}) |>
@df scatter(:b, :d)